package com.guide.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.guide.bizdto.IndexSel;
import com.guide.dao.GuideDao;
import com.guide.domain.Guide;
import com.guide.domain.GuideQuery;
import com.guide.domain.GuideQuery.Criteria;
import com.guide.domain.Personal;
import com.guide.domain.PersonalQuery;
import com.guide.domain.TravelTeam;
import com.guide.persistence.GuideMapper;
@Repository("guideDao")
public class GuideDaoImpl implements GuideDao{

	@Autowired
	private GuideMapper guideMapper;
	@Override
	public long insertGuide(Guide guide) {
		// TODO Auto-generated method stub
		return guideMapper.insert(guide);
	}
	@Override
	public Guide getGuideByUserId(Long userId) {
		GuideQuery query = new GuideQuery();
		query.createCriteria().andUserIdEqualTo(userId);
		List<Guide> guides = guideMapper.selectByExampleWithBLOBs(query);
		if(!CollectionUtils.isEmpty(guides)){
			return guides.get(0);
		}
		return null;
	}
	@Override
	public void updateByPrimaryKeySelective(Guide guide) {
		// TODO Auto-generated method stub
		guideMapper.updateByPrimaryKeySelective(guide);
	}
	@Override
	public Guide getGuideById(Long id) {
		// TODO Auto-generated method stub
		return guideMapper.selectByPrimaryKey(id);
	}
	@Override
	public List<Guide> getGuideListByIndex(IndexSel indexSel) {
		// TODO Auto-generated method stub
		String city=null;
		if(!StringUtils.isEmpty(indexSel.getCity())){
			city = indexSel.getCity();
		}
		return guideMapper.selectByExampleWithBLOBsForIndex(city, indexSel.getPageIndex()*indexSel.getPageSize(), indexSel.getPageSize());
	}
	@Override
	public int countGuideListByIndex(IndexSel indexSel) {
		// TODO Auto-generated method stub
		String city=null;
		if(!StringUtils.isEmpty(indexSel.getCity())){
			city = indexSel.getCity();
		}
		return guideMapper.countByExampleWithBLOBsForIndex(city);
	}
	@Override
	public Guide getGuideByIdnum(String idnum) {
		// TODO Auto-generated method stub
		GuideQuery query = new GuideQuery();
		query.createCriteria().andIdnumEqualTo(idnum);
		List<Guide> guideList = guideMapper.selectByExample(query);
		if(!CollectionUtils.isEmpty(guideList)) {
			return guideList.get(0);
		}
		return null;
	}
	@Override
	public List<Guide> getGuideByTeamCity(TravelTeam team) {
		// TODO Auto-generated method stub
		GuideQuery query = new GuideQuery();
		query.createCriteria().andCityEqualTo(team.getSourcecity());
		Criteria criteria = query.createCriteria();
		criteria.andCityEqualTo(team.getTargetcity());
		query.or(criteria);
		
		return guideMapper.selectByExample(query);
	}
	
	

}
